package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.Impl.BaseServiceImpl;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.vo.ShopVo;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mochen
 * @since 2023-03-23
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {

        //        参数校验
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("请输入店铺名称");
        }
        //        校验密码
        if (shop.getAdmin().getPassword() == null || shop.getAdmin().getConfirmPassword() == null){
            throw new BusinessException("请确认密码是否填写完整!");
        }
        //        todo baiduAI审核

//        if (BaiduAuditUtils.ImgCensor(shop.getLogo())){
//            throw new BusinessException("店铺商标违规!");
//        }
        shop.setState(0);
        shop.setRegisterTime(new Date());

        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getConfirmPassword())){
            throw new BusinessException("请确认密码是否填写一致!");
        }

        //        校验店铺是否存在
        Shop oldShop = shopMapper.loadByName(shop.getName());
        if (oldShop!= null){
            throw new BusinessException("店铺名重复!");
        }
        //审核通过
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称违规!");
        }
        //        校验员工是否已经存在
        Employee employee = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        //           存在
        if (employee != null){
            //           校验员工是否已经有店铺
            //           有
            if (employee.getShop_id() != null){
                throw new BusinessException("该员工已拥有店铺!");
            }else {     //没有
                shop.setAdminId(employee.getId());  //给该店铺设置员工
                shopMapper.save(shop);
                employee.setShop_id(shop.getId());  //把店铺id更新给员工
                employeeMapper.update(employee);
            }
        }
        //           不存在
        shop.getAdmin().setState(0);            //设置员工状态    0 出差    1 在职
        employeeMapper.save(shop.getAdmin());   //保存员工
        shop.setAdminId(shop.getAdmin().getId());   //给员工设置自增id
        shopMapper.save(shop);                      //保存店铺
        shop.getAdmin().setShop_id(shop.getId());   //给店铺设置管理员
        employeeMapper.update(shop.getAdmin());     //更新员工下属店铺

       /* employeeMapper.save(employee);  //保存员工*/
       /* employee.setState(0);           //更新员工状态    0 出差    1 在职*/
       /* shop.setAdminId(employee.getId());      //给店铺设置管理员*/
       /* shopMapper.save(shop);                  //保存店铺*/
       /* employee.setShop_id(shop.getId());      //更新员工管理的店铺*/
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        if (shop == null){
            throw new BusinessException("数据异常!");
        }
        //更新店铺状态
        shop.setState(1);
        shopMapper.update(shop);
        //保存审核日志
        shopAuditLog.setAuditId(335L);          //TODO
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);
        //发送激活邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1651314526@qq.com");
        //邮件主题
        mailMessage.setSubject("欢迎加入宠物大联盟!");
        //邮件内容
        mailMessage.setText("恭喜您申请的店铺已通过审核!请点击该链接进行访问:"+
                "http://localhost:7000/shop/active?shopId="+shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo("1184508554@qq.com"); //TODO

        javaMailSender.send(mailMessage);

    }

    @Override
    public void active(Long id) {
        //根据shopId查待审核店铺
        Shop waitingShop = shopMapper.loadById(id);
        //根据待审核店铺adminId查员工
        Employee employee = employeeMapper.loadById(waitingShop.getAdminId());
        //设置并更新员工状态
        employee.setState(1);
        employeeMapper.update(employee);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        if (shop == null){
            throw new BusinessException("数据异常!");
        }
        //更新店铺状态
        shop.setState(-1);
        shopMapper.update(shop);
        //保存审核日志
        shopAuditLog.setAuditId(335L);          //TODO
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);
        //发送激活邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1651314526@qq.com");
        //邮件主题
        mailMessage.setSubject("滴~您有一条来自宠物大联盟的消息!");
        //邮件内容
        mailMessage.setText("抱歉,您申请的店铺:   "+shop.getName()+"   未通过审核,请点击链接进行修改,期待您的加入! " +
                "http://localhost:7001/#/Register2?id="+shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo("1184508554@qq.com"); //TODO

        javaMailSender.send(mailMessage);
    }

    @Override
    public void changeState(Shop shop) {
        shop.setState(0);
        shopMapper.update(shop);
    }

    @Override
    public HashMap<String, ArrayList<Integer>> loadStateStatistics() {
        List<ShopVo> vos = shopMapper.loadStateStatistics();

        ArrayList<Integer> states = new ArrayList<>();
        ArrayList<Integer> nums = new ArrayList<>();

        for (ShopVo vo : vos) {
            states.add(vo.getState());
            nums.add(vo.getNum());
        }

        HashMap<String,ArrayList<Integer>> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);

        return map;
    }


}
